import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FetchService } from '../../../core/net/fetch.service';
import { NzMessageService } from 'ng-zorro-antd';
import * as _ from 'lodash';
import {GeneralSettings} from "../GeneralSettings";
import {forkJoin} from "rxjs";
import {RegionMngModel, SearchMaintainService} from "../search-maintain.service";

@Component({
  selector: 'app-mt-drain',
  templateUrl: './mt-drain.component.html',
  styleUrls: ['./mt-drain.component.scss', '../search-maintain.component.scss']
})
export class MtDrainComponent implements OnInit {
    pageNum = 1;
    pageSize = 10;
    total: number;
    tableData = [];
    loading = true;

    searchForm: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    detailForm: FormGroup;
    nzBodyStyle: any = {
        padding: '16px 20px 0'
    };
    statOpt: any = {};

    props: any;
    commonSelection: RegionMngModel;     latestList: Array<any> = [];
    constructor(private fb: FormBuilder,
                private fetchService: FetchService,
                private message: NzMessageService,
                private searchService: SearchMaintainService,
                private params: GeneralSettings) {
        this.props = this.params;
    }

    ngOnInit() {
        this.searchForm = this.fb.group({
            drainageName: ['']
        });
        this.detailForm = this.fb.group({
            id: [''],
            drainageName : ['', Validators.required],
            drainageCode: [''],
            manageCode: [''],
            constructionYear: [''],
            type: [''],
            designedDrainageCapacity: [''],
            actualDrainageCapacity: [''],
            controllingIrrigatedArea: [''],
            length: [''],
            crossCanal: [''],
            crossCoordinate: [''],
            crossType: [''],
            source: [''],
            insertRiver: [''],
            crossCounty: [''],
        });
        this.latestList = this.searchService.getLatestList('mtDrain');

        this.searchService.regionMngDataChange.subscribe(data => {
            this.commonSelection = data;
            this.getGridData();
        });
    }
    getLatestData(data): void {
        this.searchForm.patchValue({
            drainageName: data.keyword
        });
        this.getGridData(true);
    }
    openEditModal(item?): void {
        this.modalTitle = item ? `编辑${item.drainageName}` : '新增排水沟';
        item ? this.detailForm.patchValue(item) : this.detailForm.reset();
        this.modalShow = true;
    }

    onDelete(id) {
        this.fetchService.get(`wrs/wrsBrdrainageDitch/remove/${id}`).subscribe(resp => {
            this.message.info('删除成功');
            this.getGridData();
        });
    }

    onConfirm() {
        if (!this.detailForm.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = this.detailForm.value;
        if (data.id) {
            this.fetchService.post('wrs/wrsBrdrainageDitch/edit', data).subscribe(res => {
                this.message.success('修改成功');
                this.modalShow = false;
                this.getGridData();
            });
        } else {
            this.fetchService.post('wrs/wrsBrdrainageDitch/add', data).subscribe(res => {
                this.message.success('新增成功');
                this.modalShow = false;
                this.getGridData();
            });
        }
    }

    onCancel() {
        this.modalShow = false;
    }

    getGridData(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.latestList = this.searchService.getLatestList('mtDrain');
        if (this.searchForm.value.drainageName) {
            this.searchService.setLatestList('mtDrain', this.searchForm.value.drainageName);
        }
        const params = _.assign(_.cloneDeep(this.searchForm.value), this.commonSelection, {
            pageNum: this.pageNum,
            pageSize: this.pageSize
        });
        const queryTableData = this.fetchService.post('wrs/wrsBrdrainageDitch/list', params);
        const queryPanelData = this.fetchService.post('wrs/wrsBrdrainageDitch/list', _.assign(_.cloneDeep(this.searchForm.value), this.commonSelection, {
            pageSize: 100000
        }));
        forkJoin(queryTableData, queryPanelData).subscribe(res => {
            this.loading = false;
            if (res) {
                this.total = res[0]['total'];
                this.tableData = res[0]['list'];
                const baseList = res[1]["list"];
                this.statOpt = {
                    statType: 'paishuigou',
                    baseList
                };
            }
        });
    }
}
